package com.woniu.gym.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.core.exception.gym.GymException;
import com.woniu.core.exception.gym.GymExceptionCode;
import com.woniu.core.util.PageInfo;
import com.woniu.gym.client.AccountServerClient;
import com.woniu.gym.client.dto.GymAccountDto;
import com.woniu.gym.dao.TGymBaseMapper;
import com.woniu.gym.dto.FindGymBaseDto;
import com.woniu.gym.dto.GymBaseDto;
import com.woniu.gym.form.FindGymBaseByAdminForm;
import com.woniu.gym.form.FindGymBaseByIdForm;
import com.woniu.gym.form.ModifyGymInformationForm;
import com.woniu.gym.model.TGymBase;
import com.woniu.gym.service.TGymBaseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年04月02日
 */
@Service
public class TGymBaseServiceImpl extends ServiceImpl<TGymBaseMapper, TGymBase> implements TGymBaseService {
    @Resource
    private TGymBaseMapper tGymBaseMapper;
    @Resource
    private AccountServerClient accountServerClient;
    @Override
    public GymBaseDto findGymBaseByNum(String gymNum) {
        QueryWrapper<TGymBase> wrapper = new QueryWrapper<>();
        wrapper.eq("gym_num",gymNum);
        TGymBase gymBase = tGymBaseMapper.selectOne(wrapper);
        if(gymBase==null){
            throw new GymException(GymExceptionCode.GYM_NOT_EXIST);
        }
        GymBaseDto gymBaseDto = BeanUtil.toBean(gymBase, GymBaseDto.class);
        return gymBaseDto;

    }

    @Override
    public TGymBase modifyGymMark(String gymNum, Double gymMark) {
        QueryWrapper<TGymBase> wrapper = new QueryWrapper<>();
        wrapper.eq("gym_num",gymNum);
        TGymBase gymBase = tGymBaseMapper.selectOne(wrapper);
        if(gymBase==null ){
            throw new GymException(GymExceptionCode.GYM_NOT_EXIST);
        }
        // 验证评分数据
        if (gymMark < 1 || gymMark > 5) {
            throw new GymException("请输入1-5之间的评分数据",50001);
        }
        //新的评价总人数
        int num = gymBase.getMarkNum()+1;
        //新的评分((原来的评分*原来的人数+新的评分)/新的总评价人数)
        Double newMark = (gymBase.getGymMark()*gymBase.getMarkNum()+gymMark)/num;
        gymBase.setGymMark(newMark);
        gymBase.setMarkNum(num);
        tGymBaseMapper.updateById(gymBase);

        return gymBase;
    }

    @Override
    public PageInfo<FindGymBaseDto> findGymBaseById(FindGymBaseByIdForm form) {
        QueryWrapper<TGymBase> wrapper = new QueryWrapper<>();
        wrapper.eq("gym_id",form.getGymId());
        List<TGymBase> tGymBases = tGymBaseMapper.selectList(wrapper);
        if(tGymBases==null || tGymBases.size()<1 ){
            throw new GymException(GymExceptionCode.GYM_NOT_EXIST);
        }
        Page<TGymBase> page = new Page<>(form.getPageNum()==null?1:form.getPageNum(),form.getPageSize()==null?5:form.getPageSize());

        if(ObjectUtil.isAllNotEmpty(form.getGymState())){
            wrapper.eq("gym_state",form.getGymState());
        }
        if(ObjectUtil.isAllNotEmpty(form.getBusinessState())){
            wrapper.eq("business_state",form.getBusinessState());
        }
        tGymBaseMapper.selectPage(page,wrapper);
        List<TGymBase> list = page.getRecords();
        ArrayList<FindGymBaseDto> data = new ArrayList<>();
        for (TGymBase gymBase : list) {
            GymAccountDto gymAccountDto = accountServerClient.findGymAccountById(gymBase.getGymId()).getData();
            FindGymBaseDto dto = BeanUtil.toBean(gymBase, FindGymBaseDto.class);
            dto.setAccountName(gymAccountDto.getAccountName());
            data.add(dto);
        }
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setList(data);
        return pageInfo;
    }

    @Override
    public PageInfo<FindGymBaseDto> findGymBaseByAdmin(FindGymBaseByAdminForm form) {
        QueryWrapper<TGymBase> wrapper = new QueryWrapper<>();
        Page<TGymBase> page = new Page<>(form.getPageNum()==null?1:form.getPageNum(),form.getPageSize()==null?5:form.getPageSize());
        if(ObjectUtil.isAllNotEmpty(form.getGymName())){
            wrapper.like("gym_name",form.getGymName());
        }
        if(ObjectUtil.isAllNotEmpty(form.getGymState())){
            wrapper.eq("gym_state",form.getGymState());
        }
        if(ObjectUtil.isAllNotEmpty(form.getBusinessState())){
            wrapper.eq("business_state",form.getBusinessState());
        }
        tGymBaseMapper.selectPage(page,wrapper);
        List<TGymBase> list = page.getRecords();
        ArrayList<FindGymBaseDto> data = new ArrayList<>();
        for (TGymBase gymBase : list) {
            GymAccountDto gymAccountDto = accountServerClient.findGymAccountById(gymBase.getGymId()).getData();
            FindGymBaseDto dto = BeanUtil.toBean(gymBase, FindGymBaseDto.class);
            dto.setAccountName(gymAccountDto.getAccountName());
            data.add(dto);
        }
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setList(data);
        return pageInfo;
    }

    @Override
    public TGymBase modifyGymByNum(ModifyGymInformationForm form) {
        QueryWrapper<TGymBase> wrapper = new QueryWrapper<>();
        wrapper.eq("gym_num",form.getGymNum());
        TGymBase tGymBase = tGymBaseMapper.selectOne(wrapper);
        if(tGymBase==null ){
            throw new GymException(GymExceptionCode.GYM_NOT_EXIST);
        }
        tGymBase.setGymPhoto(form.getGymPhoto());
        tGymBase.setGymDetails(form.getGymDetails());
        tGymBase.setBusinessState(form.getBusinessState());
        tGymBaseMapper.updateById(tGymBase);
        return tGymBase;
    }

    @Override
    public TGymBase modifyGymState(String gymNum) {
        QueryWrapper<TGymBase> wrapper = new QueryWrapper<>();
        wrapper.eq("gym_num",gymNum);
        TGymBase tGymBase = tGymBaseMapper.selectOne(wrapper);
        if(tGymBase==null ){
            throw new GymException(GymExceptionCode.GYM_NOT_EXIST);
        }
        tGymBase.setGymState(1);
        tGymBase.setInMoney(1);
        tGymBaseMapper.updateById(tGymBase);
        return tGymBase;
    }
}
